<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Welcome extends CI_Controller {

    var $uid = null;

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index() {
        $this->load->library('facebook', $this->config);
        $this->load->helper('facebook');
        if (helper_checkUserLogin($this->facebook, $this->config->config)) {
            $config = $this->config->config;
            if (isset($_REQUEST['state'])) {
                helper_redirectTop($config['redirect_uri_page']);
            }
            $this->uid = $this->facebook->getUser();
            $this->user->setUserInfo($this->getUserInfo());
            $this->user->insert_user();
            helper_redirect("./index.php/view/viewlanding");
        }
    }

    public function addVote() {
        if (!isset($this->uid)) {
            $this->uid = $this->facebook->getUser();
        }
        if ($this->uid != 0) {
            $pid = $_REQUEST['pid'];
            $this->vote->setVoteInfo($this->uid, $pid);
            $this->vote->insert_vote();
        }
    }

    function getUserInfo() {
        $UserInfoArray = array();

        $fql = "SELECT uid,email FROM user WHERE uid = me()";

        $param = array(
            'method' => 'fql.query',
            'query' => $fql,
            'callback' => ''
        );

        $UserInfoArray = $this->facebook->api($param);
        $UserInfo = $UserInfoArray[0];
        return $UserInfo;
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */